home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 05 / 32gdi / plgblt.c < prev    next >
C/C++ Source or Header  |  1992-09-01  |  4KB  |  142 lines

  1. /*------------------------------------------
  2.    PLGBLT.C -- Win32 Rotated Bitmap Display
  3.                (c) Charles Petzold, 1992
  4.   ------------------------------------------*/
  5.  
  6. #define OEMRESOURCE
  7. #include <windows.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10.  
  11. LONG APIENTRY WndProc (HWND, UINT, DWORD, LONG) ;
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                     LPSTR lpszCmdParam, int nCmdShow)
  15.      {
  16.      static char szAppName[] = "PlgBlt" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASS    wndclass ;
  20.  
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.  
  32.      RegisterClass (&wndclass) ;
  33.  
  34.      hwnd = CreateWindow (szAppName, "Rotated Bitmap Display",
  35.                           WS_OVERLAPPEDWINDOW,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           CW_USEDEFAULT, CW_USEDEFAULT,
  38.                           NULL, NULL, hInstance, NULL) ;
  39.  
  40.      ShowWindow (hwnd, nCmdShow) ;
  41.      UpdateWindow (hwnd) ;
  42.  
  43.      while (GetMessage (&msg, NULL, 0, 0))
  44.           {
  45.           TranslateMessage (&msg) ;
  46.           DispatchMessage (&msg) ;
  47.           }
  48.      return msg.wParam ;
  49.      }
  50.  
  51. HBITMAP LoadChessBitmap (HDC hdc)
  52.      {
  53.      BITMAPFILEHEADER bf ;
  54.      BYTE *           pBuffer ;
  55.      FILE *           file ;
  56.      HBITMAP          hbm ;
  57.  
  58.      if (NULL == (file = fopen ("\\nt\\windows\\chess.bmp", "rb")))
  59.           return NULL ;
  60.  
  61.      if (1 != fread (&bf, sizeof (BITMAPFILEHEADER), 1, file))
  62.           {
  63.           fclose (file) ;
  64.           return NULL ;
  65.           }
  66.  
  67.      if (NULL == (pBuffer = malloc (bf.bfSize)))
  68.           {
  69.           fclose (file) ;
  70.           return NULL ;
  71.           }
  72.  
  73.      fread (pBuffer, bf.bfSize, 1, file) ;
  74.      fclose (file) ;
  75.  
  76.      hbm = CreateDIBitmap (hdc, (PBITMAPINFOHEADER) pBuffer, CBM_INIT,
  77.                            pBuffer + bf.bfOffBits - sizeof (BITMAPFILEHEADER),
  78.                            (PBITMAPINFO) pBuffer, DIB_RGB_COLORS) ;
  79.      free (pBuffer) ;
  80.  
  81.      return hbm ;
  82.      }
  83.  
  84. LONG APIENTRY WndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam)
  85.      {
  86.      static HBITMAP hbmChess ;
  87.      static short   cxClient, cyClient ;
  88.      HDC            hdc, hdcMem ;
  89.      PAINTSTRUCT    ps ;
  90.      POINT          apt [3] ;
  91.  
  92.      switch (message)
  93.           {
  94.           case WM_CREATE:
  95.                hdc = GetDC (hwnd) ;
  96.  
  97.                hbmChess = LoadChessBitmap (hdc) ;
  98.  
  99.                ReleaseDC (hwnd, hdc) ;
  100.                return 0 ;
  101.  
  102.           case WM_SIZE:
  103.                cxClient = LOWORD (lParam) ;
  104.                cyClient = HIWORD (lParam) ;
  105.                return 0 ;
  106.  
  107.           case WM_PAINT:
  108.            hdc = BeginPaint (hwnd, &ps) ;
  109.  
  110.                if (hbmChess)
  111.                     {
  112.                     hdcMem = CreateCompatibleDC (hdc) ;
  113.                     SelectObject (hdcMem, hbmChess) ;
  114.  
  115.                     apt[0].x = 0 ;
  116.                     apt[0].y = cyClient / 2 ;
  117.  
  118.                     apt[1].x = cxClient / 2 ;
  119.                     apt[1].y = 0 ;
  120.  
  121.                     apt[2].x = cxClient / 2 ;
  122.                     apt[2].y = cyClient ;
  123.  
  124.                     PlgBlt (hdc, apt, hdcMem, 0, 0, 640, 480, NULL, 0, 0) ;
  125.  
  126.                     DeleteDC (hdcMem) ;
  127.                     }
  128.  
  129.            EndPaint (hwnd, &ps) ;
  130.                return 0 ;
  131.  
  132.           case WM_DESTROY:
  133.                if (hbmChess)
  134.                     DeleteObject (hbmChess) ;
  135.  
  136.                PostQuitMessage (0) ;
  137.                return 0 ;
  138.           }
  139.  
  140.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  141.      }
  142.